package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.CustomException;

import com.itheima.ydd.config.R;
import com.itheima.ydd.dto.BaseInfoDto;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.service.ISysConfigService;
import com.itheima.ydd.service.ISysUserService;
import com.itheima.ydd.service.indexService;
import com.itheima.ydd.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
管理员登录
 */
@RestController
@RequestMapping("/index")
public class IndexContorller {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private indexService indexService;

    @Autowired
    private ISysConfigService configService;



    @PostMapping("/login")
    public R<Object> login (@RequestBody Map map, HttpServletRequest request){

        String loginName = (String) map.get("login_name");

        String password = (String) map.get("password");

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getLoginName,loginName);
        wrapper.eq(SysUser::getDelFlag,"0");

        SysUser sysUser = userService.getOne(wrapper);
        //判断用户是否存在
        if (sysUser==null){
            //交给业务异常处理
            throw  new CustomException("用户名或密码错误");
        }
        if (sysUser.getStatus()==0){
            throw new CustomException("账号被冻结");
        }
         password = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断密码是否正确

        if (password.equals(sysUser.getPassword())){
            R<Object> r = new R<>();

            Map<String, String> hashMap = new HashMap<>();
            Map<String, String> tokenMap = new HashMap<>();
            tokenMap.put("userId",sysUser.getId());

            String token = JwtUtils.getToken(tokenMap);
            hashMap.put("token",token);
            return R.success(hashMap);
        }
        throw new CustomException("用户名或密码错误");
    }


    @PostMapping("/profile")
    public R<Map> profile (HttpServletRequest request){


        String token = request.getHeader("x-token");
        Claims claims = JwtUtils.getClaims(token);
        String userId = (String) claims.get("userId");

        Map<String, Object> profile = indexService.getProfile(userId);

         return R.success(profile);

    }



    @GetMapping("/getConfig")
    public R<Map> getConfig (){

        Map<String,String> map = new HashMap<>();
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SysConfig::getTabValue,"system");

        List<SysConfig> sysConfigs = configService.list(queryWrapper);
        for (SysConfig sysConfig : sysConfigs) {
            map.put(sysConfig.getAttrKey(),sysConfig.getAttrValue());
        }
        return R.success(map);
    }

    @GetMapping("/baseInfo")
    public R<BaseInfoDto>baseInfo(){

        BaseInfoDto baseInfoDto = indexService.baseInfo();
        return R.success(baseInfoDto);

    }



}
